package cn.t222.ch04.config;

import cn.t222.ch04.ex.CustomErrorController;
import cn.t222.ch07.starter.MyStarter;
import cn.t222.ch07.starter.MyStarterProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.*;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
//    @Bean
//    CustomErrorController errorController(ErrorAttributes errorAttributes,
//                                          ServerProperties serverProperties,
//                                          List<ErrorViewResolver> errorViewResolvers) {
//        return new CustomErrorController(errorAttributes,serverProperties.getError(), errorViewResolvers);
//    }
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //使用模板时候不允许直接访问html
        registry.addViewController("/tologin").setViewName("/login");
    }
//    @Bean
//    MyStarter myStarter(@Autowired MyStarterProperties myStarterProperties) {
//        return new MyStarter(myStarterProperties);
//    }

    @Bean
    FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        /*filterRegistrationBean.setFilter(new Filter() {
            @Override
            public void doFilter(ServletRequest servletRequest,
                                 ServletResponse servletResponse,
                                 FilterChain filterChain) throws IOException, ServletException {
                System.out.println("在过滤器中。。。。");
                if(true) throw new NullPointerException("自己抛出的异常");
                filterChain.doFilter(servletRequest, servletResponse);
            }
        });*/
        filterRegistrationBean.setFilter((request, response, chain) -> {
            System.out.println("在过滤器中2。。。。");
            request.setCharacterEncoding("UTF-8");
//            if(true) throw new NullPointerException("自己抛出的异常");
            chain.doFilter(request, response);
        });
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setOrder(1);
        return filterRegistrationBean;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/img/**")
                .addResourceLocations("file:C:/upload/");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowCredentials(true)
                .allowedOrigins("*")
                .allowedHeaders("*")
                .allowedMethods("*");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new HandlerInterceptor() {
            @Override
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
                System.out.println("进入了拦截器:" + request.getRequestURI());
                return true;
            }
        })
        .addPathPatterns("/**")
        .excludePathPatterns("/test");
    }
}
